package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyCondition;
import cn.wolfcode.wolf2w.business.mapper.StrategyConditionMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.query.StrategyConditionQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyConditionService;
import com.alibaba.druid.proxy.jdbc.JdbcParameter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 攻略条件Service业务层处理
 * 
 * @author hyz
 * @date 2024-07-26
 */
@Service
@Transactional
public class StrategyConditionServiceImpl extends ServiceImpl<StrategyConditionMapper,StrategyCondition> implements IStrategyConditionService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyCondition> queryPage(StrategyConditionQuery qo) {
        IPage<StrategyCondition> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCondition> queryConditionByType(Integer type) {
        List<StrategyCondition> list = lambdaQuery().inSql(StrategyCondition::getStatisTime,"select MAX(statis_time) from ta_strategy_condition")
                .eq(StrategyCondition::getType, type)
                .orderByDesc(StrategyCondition::getCount)
                .list();
        return list;
    }

    @Override
    public void updateStrategyCondition(int type, Date date) {
        QueryWrapper<Strategy> qw = new QueryWrapper<>();
        if(type == 1 || type == 2){
            qw.eq("isabroad", type==1?1:0);
            qw.groupBy("dest_id, dest_name");
            qw.select("dest_id refid, dest_name name, count(*) count");
            List<Map<String, Object>> maps = strategyMapper.selectMaps(qw);
            List<StrategyCondition> conditions = new ArrayList<>();
            for (Map<String, Object> map : maps) {
                Long refid = (Long) map.get("refid");
                String name = (String) map.get("name");
                long count = (long) map.get("count");
                StrategyCondition condition = new StrategyCondition();
                condition.setRefid(refid);
                condition.setName(name);
                condition.setCount(count);
                condition.setType((long) type);
                condition.setStatisTime(date);
                conditions.add(condition);
            }
            saveBatch(conditions);
        }else if(type == 3){
            qw.groupBy("theme_id, theme_name");
            qw.select("theme_id refid, theme_name name, count(*) count");
            List<Map<String, Object>> maps = strategyMapper.selectMaps(qw);
            List<StrategyCondition> conditions = new ArrayList<>();
            for (Map<String, Object> map : maps) {
                Long refid = (Long) map.get("refid");
                String name = (String) map.get("name");
                long count = (long) map.get("count");
                StrategyCondition condition = new StrategyCondition();
                condition.setRefid(refid);
                condition.setName(name);
                condition.setCount(count);
                condition.setType((long) type);
                condition.setStatisTime(date);
                conditions.add(condition);
            }
            saveBatch(conditions);
        }
    }
}
